﻿using System;
using System.Security.Cryptography;
using System.Text;

namespace AZMapDBHelperPlugin
{
    public class EncryptString
    {
        private const string manifesto = "AZMap is great and Powerful, All maps downloader.";

        public EncryptString()
        {
        }

        private static string Decrypt(string Message, string Passphrase)
        {
            byte[] buffer3;
            UTF8Encoding encoding = new UTF8Encoding();
            using (MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider())
            {
                byte[] buffer = provider.ComputeHash(encoding.GetBytes(Passphrase));
                using (TripleDESCryptoServiceProvider provider2 = new TripleDESCryptoServiceProvider())
                {
                    provider2.Key = buffer;
                    provider2.Mode = CipherMode.ECB;
                    provider2.Padding = PaddingMode.PKCS7;
                    byte[] inputBuffer = Convert.FromBase64String(Message);
                    try
                    {
                        using (ICryptoTransform transform = provider2.CreateDecryptor())
                        {
                            buffer3 = transform.TransformFinalBlock(inputBuffer, 0, inputBuffer.Length);
                        }
                    }
                    finally
                    {
                        provider2.Clear();
                        provider.Clear();
                    }
                }
            }
            return encoding.GetString(buffer3, 0, buffer3.Length);
        }

        private static string Encrypt(string Message, string Passphrase)
        {
            byte[] buffer3;
            UTF8Encoding encoding = new UTF8Encoding();
            using (MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider())
            {
                byte[] buffer = provider.ComputeHash(encoding.GetBytes(Passphrase));
                using (TripleDESCryptoServiceProvider provider2 = new TripleDESCryptoServiceProvider())
                {
                    provider2.Key = buffer;
                    provider2.Mode = CipherMode.ECB;
                    provider2.Padding = PaddingMode.PKCS7;
                    byte[] bytes = encoding.GetBytes(Message);
                    try
                    {
                        using (ICryptoTransform transform = provider2.CreateEncryptor())
                        {
                            buffer3 = transform.TransformFinalBlock(bytes, 0, bytes.Length);
                        }
                    }
                    finally
                    {
                        provider2.Clear();
                        provider.Clear();
                    }
                }
            }
            return Convert.ToBase64String(buffer3);
        }

        public static string GetEncryptString(string Message)
        {
            return Encrypt(Message, "AZMap is great and Powerful, All maps downloader.");
        }

        public static string ThisIsLegalString(string Message)
        {
            return Decrypt(Message, "AZMap is great and Powerful, All maps downloader.");
        }
    }
}
